<template>
  <div class="app-container">
    <el-dialog title="现货下单" :visible.sync="dialogVisible" width="60%" append-to-body>
      <div v-loading="loading">
        <el-form ref="form" :model="form">
          <div class="myTitle">基本信息</div>
          <el-row :gutter="10">
            <el-col :span="6">
              <el-form-item label="订单编号" prop="orderNo">
                <el-input v-model="form.orderNo" placeholder="自动生成" disabled/>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="来源平台" prop="orderFrom" required>
                <el-select v-model="form.orderFrom" filterable style="width: 100%">
                  <el-option
                    v-for="item in dict.type.ims_source_platform"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="源平台单号" prop="sourceOrderNo">
                <el-input v-model="form.sourceOrderNo" placeholder="请输入"/>
              </el-form-item>
            </el-col>
          </el-row>
          <div>
            <div class="myTitle">商品信息</div>
            <el-table
              :data="shopList"
              max-height="500"
            >
              <el-table-column type="index" label="序号" align="center"></el-table-column>
              <el-table-column prop="imageUrl" label="图片" align="center">
                <template slot-scope="scope">
                  <div v-if="!scope.row.imageUrl">暂无</div>
                  <el-image
                    v-else
                    style="width: 80px;height: 80px"
                    :src="scope.row.imageUrl"
                    fit="contain"
                    :preview-src-list="[scope.row.imageUrl]"
                  >
                  </el-image>
                </template>
              </el-table-column>
              <el-table-column prop="title" label="商品/套餐名称" align="center" width="400"/>
              <el-table-column prop="priceType" label="类型" align="center">
                <template slot-scope="scope">
                  <dict-tag :options="dict.type.ats_price_type" :value="scope.row.priceType"/>
                </template>
              </el-table-column>
              <el-table-column prop="unitPrice" label="单价(元)" align="center"/>
              <el-table-column prop="quantity" label="数量" align="center" />
            </el-table>
          </div>
          <div style="display: flex;margin-top: 20px">
            <div class="myTitle">收件人信息</div>
            <el-button type="primary" plain @click="getAddrMsg" style="margin-left: 20px">地址识别</el-button>
          </div>
          <el-row :gutter="10">
            <el-col :span="16">
              <el-form-item label="收件人信息" prop="recipientContent" required>
                <el-input type="textarea" v-model="form.recipientContent"
                          placeholder="参考格式：张三**省**市**区**街道**小区13088888888"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :span="6">
              <el-form-item label="姓名" prop="recipient" required>
                <el-input v-model="form.recipient" placeholder="请输入"/>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="电话" prop="recipientTel" required>
                <el-input v-model="form.recipientTel" placeholder="请输入"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="详细地址" prop="fullName" required>
                <el-input v-model="form.fullName" placeholder="请输入"/>
              </el-form-item>
            </el-col>
          </el-row>
          <div>
            <div style="display:flex;">
              <div class="myTitle">付款信息</div>
            </div>
            <el-row :gutter="10">
              <el-col :span="6">
                <el-form-item label="合计价格(元)" prop="totalPrice">
                  <div>{{ form.totalPrice || 0 }}</div>
                </el-form-item>
              </el-col>
            </el-row>
          </div>
          <el-row :gutter="10">
            <el-col :span="12">
              <el-form-item label="备注" prop="remark">
                <el-input type="textarea" v-model="form.remark" placeholder="请输入"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :span="12">
              <el-form-item label="出单备注" prop="printRemark">
                <el-input type="textarea" v-model="form.printRemark" placeholder="请输入"/>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible=false">取 消</el-button>
        <el-button type="primary" @click="dialogOk">下 单</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  submitCustomOrder,
  getCommonCode, getAddrMsg
} from "../../../api/imsMng";
import {addShopCar} from "../../../api/saleMng";


export default {
  dicts: ['ims_source_platform','ats_price_type'],
  data() {
    return {
      loading: false,
      dialogVisible: false,
      form: {},
      shopList: []
    }
  },
  methods: {
    show(list) {
      this.dialogVisible = true
      this.form = {}
      this.resetForm('form')
      this.$set(this.form, 'orderType', '20')
      this.shopList=list
      this.getPrice(list)
      getCommonCode({orderType: 20}).then(res => {
        this.$set(this.form, 'orderNo', res.data.orderNo)
      })
    },
    getAddrMsg() {
      let req = {
        recipientContent: this.form.recipientContent
      }
      this.loading = true
      getAddrMsg(req).then(res => {
        let data = res.data
        this.$set(this.form, 'recipient', data.recipient)
        this.$set(this.form, 'recipientTel', data.recipientTel)
        this.$set(this.form, 'fullName', data.fullName)
      }).finally(e => {
        this.loading = false
      })
    },
    getPrice(list) {
      let totalPrice=0
      list.map(val=>{
        totalPrice=totalPrice+((Number(val.unitPrice) || 0) * (Number(val.quantity) || 0))
      })
      this.$set(this.form, 'totalPrice', totalPrice)
    },
    dialogOk() {
      this.$refs.form.validate(e => {
        if (e) {
          let req = JSON.parse(JSON.stringify(this.form))
          req.customerOrderMatPartBoList = this.shopList
          this.loading = true
          submitCustomOrder(req).then(res => {
            this.$emit('ok')
            this.dialogVisible = false
            this.$message.success('下单成功')
            this.$router.push('/orderMng/orderMng?flag=20')
          }).finally(e => {
            this.loading = false
          })
        }
      })
    }
  }
}
</script>
